﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EHTWebsite.Code;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Code.BLL
{
    public class ModulProvider
    {
        public static DataSet GetAllModul(Boolean Choose, String Nhom, Boolean TrangThai)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetAllModul";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@Choose", Choose));
            command.Parameters.Add(new SqlParameter("@Nhom", Nhom));
            command.Parameters.Add(new SqlParameter("@TrangThai", TrangThai));

            return DataProvider.ExecuteDataSet(command);
        }
        
        public static void UpdateChooseModul(Int32 ModulID, Boolean Choose)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_UpdateChooseModul]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ModulID", ModulID));
            command.Parameters.Add(new SqlParameter("@Choose", Choose));

            DataProvider.ExecuteNoneQuery(command);
        }
        public static void ChangePublish(Int32 ModulID, Boolean TrangThai)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_ChangePublishModul]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ModulID", ModulID));
            command.Parameters.Add(new SqlParameter("@TrangThai", TrangThai));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateNumberOfOrder(Int32 ModulID, Int32 SapXep)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_UpdateNumberOfOrderByModulID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ModulID", ModulID));
            command.Parameters.Add(new SqlParameter("@SapXep", SapXep));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static Modul GetModulByID(Int32 ModulID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetModulByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ModulID", ModulID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            Modul theModul = new Modul(); ;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theModul.Choose = Boolean.Parse(ds.Tables[0].Rows[0]["Choose"].ToString());
                theModul.Nhom = ds.Tables[0].Rows[0]["Nhom"].ToString();
                theModul.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theModul.TenModul = ds.Tables[0].Rows[0]["TenModul"].ToString();
                theModul.TenModulOther = ds.Tables[0].Rows[0]["TenModulOther"].ToString();
                theModul.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                theModul.ModulID = Int32.Parse(ds.Tables[0].Rows[0]["ModulID"].ToString());
                theModul.TrangThai = Boolean.Parse(ds.Tables[0].Rows[0]["TrangThai"].ToString());
            }

            return theModul;
        
        }

        public static void IUModul(Modul theModul, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_UIModul]";
            command.CommandType = CommandType.StoredProcedure;
           

            command.Parameters.Add(new SqlParameter("@ModulID ", theModul.ModulID));
            command.Parameters.Add(new SqlParameter("@TenModul", theModul.TenModul));
            command.Parameters.Add(new SqlParameter("@TenModulOther", theModul.TenModulOther));
            command.Parameters.Add(new SqlParameter("@Nhom", theModul.Nhom));
            command.Parameters.Add(new SqlParameter("@SapXep", theModul.SapXep));
            command.Parameters.Add(new SqlParameter("@Url", theModul.Url));            
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);            
        }
    }
}